package com.lt.jvm;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * Copyright (C), 2006-2010, ChengDu ybyainfo. Co., Ltd.
 * FileName: WebClient.java
 *
 * @version 1.00
 * @Author lt
 * @Date 2020/5/20 11:38
 **/
public class WebClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 8000));

        ByteBuffer writeBuffer = ByteBuffer.allocate(32);
        ByteBuffer readBuffer = ByteBuffer.allocate(32);

        writeBuffer.put("hello".getBytes());
        writeBuffer.flip();

        while (true) {
            writeBuffer.rewind();
            socketChannel.write(writeBuffer);
            readBuffer.clear();
            socketChannel.read(readBuffer);
        }
//        for (int i = 0; i < 10; i++) {
//            writeBuffer.rewind();
//            socketChannel.write(writeBuffer);
//            readBuffer.clear();
//            socketChannel.read(readBuffer);
//        }
    }
}
